package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YYP
 * @date 2020/12/29
 */
@RestController
@RequestMapping("/spec")
public class SpecController {

    //依赖注入
    private SpecGroupService specGroupService;
    private SpecParamService specParamService;
    public SpecController(SpecGroupService specGroupService,SpecParamService specParamService){
        this.specGroupService = specGroupService;
        this.specParamService = specParamService;
    }


    /**
     * 根据商品分类id，查询规格组的集合
     * @param categoryId
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listGroupByCategoryId(@RequestParam("id") Long categoryId){
        return ResponseEntity.ok(specGroupService.listGroupByCategoryId(categoryId));
    }

    /**
     * 根据条件查询规格参数集合
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> listParamByQuery(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        return ResponseEntity.ok(specParamService.listParamByQuery(categoryId,groupId,searching));
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listGroupParamsByCategoryId(@RequestParam("id")Long categoryId){

        return ResponseEntity.ok(specGroupService.listGroupParamsByCategoryId(categoryId));
    }
}
